package com.raven.play.datalink.common;

import lombok.Getter;
import org.apache.avro.Schema;
import org.apache.avro.generic.IndexedRecord;

import java.util.Arrays;

/**
 * 作业组件中传递数据的格式
 * 适用于不需要知道数据类型和长度
 */
public class SimpleRecord implements IndexedRecord {

    @Getter
    private final Object[] values;

    @Getter
    private final int length;

    public SimpleRecord(int length) {
        this.length = length;
        this.values = new Object[length];
    }

    public static SimpleRecord create(int length) {
        return new SimpleRecord((length));
    }

    public SimpleRecord set(int position, Object value) {
        values[position] = value;
        return this;
    }

    @Override
    public void put(int position, Object value) {
        values[position] = value;
    }

    @Override
    public Schema getSchema() {
        return null;
    }

    public Object get(int position) {
        return values[position];
    }

    @Override
    public String toString() {
        return values == null ? "Record[]" : "Record" + Arrays.stream(this.values).toList();
    }
}
